package com.github.gaojh.config;

import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author gaojh
 * @since 2020/3/23
 */

public enum Environment {
    /**
     * 实例
     */
    me;

    private int port = 8080;

    private boolean enableWebsocket = false;

    private Properties props;

    Environment() {
        props = new Properties();
        try {
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(System.getProperty("flying.application", "application.properties"));
            if (inputStream != null) {
                props.load(inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        //再从系统配置项获取并覆盖
        initFromSystem();

        this.port = getInteger("server.port", 8080);
        this.enableWebsocket = getBoolean("flying.websocket.enable", false);
    }

    private void initFromSystem() {
        Properties properties = System.getProperties();
        properties.stringPropertyNames().forEach(key -> props.setProperty(key, properties.getProperty(key)));
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setEnableWebsocket(boolean enableWebsocket) {
        this.enableWebsocket = enableWebsocket;
    }

    public Integer getPort() {
        return this.port;
    }

    public boolean isEnableWebsocket() {
        return this.enableWebsocket;
    }

    public String getString(String key) {
        return props.getProperty(key);
    }

    public String getString(String key, String defaultValue) {
        return props.getProperty(key, defaultValue);
    }

    public Integer getInteger(String key) {
        String value = props.getProperty(key);
        if (value != null) {
            return Integer.parseInt(value);
        }
        return null;
    }

    public Integer getInteger(String key, Integer defaultValue) {
        String value = props.getProperty(key);
        if (value != null) {
            return Integer.parseInt(value);
        }
        return defaultValue;
    }

    public Long getLong(String key) {
        return (Long) props.getOrDefault(key, null);
    }

    public Long getLong(String key, Long value) {
        return (Long) props.getOrDefault(key, value);
    }

    public Double getDouble(String key) {
        return (Double) props.getOrDefault(key, null);
    }

    public Double getDouble(String key, Double value) {
        return (Double) props.getOrDefault(key, value);
    }

    public Boolean getBoolean(String key) {
        return (Boolean) props.getOrDefault(key, false);
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        return (Boolean) props.getOrDefault(key, defaultValue);
    }

    public Object get(String key) {
        return props.get(key);
    }

    public Object get(String key, Object defaultValue) {
        return props.getOrDefault(key, defaultValue);
    }
}
